//
//  XLComposePicViewCell.swift
//  SinaWeibo
//
//  Created by 李凯 on 16/9/12.
//  Copyright © 2016年 kevin. All rights reserved.
//

import UIKit

class XLComposePicViewCell: UICollectionViewCell {
    
    var deleteBtnClickClosure: (() -> ())?
    
    var image: UIImage? {
        didSet{
            if image == nil{
                imageView.image = UIImage(named: "compose_pic_add")
                imageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
            }else{
                imageView.image = image
            }
            deleteBtn.isHidden = image == nil
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        
        contentView.addSubview(imageView)
        contentView.addSubview(deleteBtn)
        //布局
        imageView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView)
        }
        deleteBtn.snp_makeConstraints { (make) in
            make.top.right.equalTo(contentView)
        }
        
    }
    
    //点击事件
    func deleteBtnClick() {
        //执行闭包
        deleteBtnClickClosure?()
    }
    
    //懒加载
    lazy var imageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        return imageView
    }()
    
    lazy var deleteBtn: UIButton = {
        let deleteBtn = UIButton()
        deleteBtn.setImage(UIImage(named: "compose_photo_close"), for: .normal)
        //添加点击事件
        deleteBtn.addTarget(self, action: #selector(deleteBtnClick), for: UIControlEvents.touchUpInside)
        return deleteBtn
    }()
}
